import axios from 'axios';
import { message } from 'antd';
import { baseUrl } from '../config';
import { Component } from "react";
import store from '../redux/store';
import { setLoading } from '../redux/actions/setting';

axios.create({
  baseURL: process.env.REACT_APP_BASE_URL,
  timeout: 10000,
  retry: 1,
  retryDelay: 1000
});

//请求拦截
axios.interceptors.request.use(
  function (config) {
    const token = localStorage.getItem('token');
    const Userid = localStorage.getItem('userId');
    config.headers['Token'] = token === undefined ? '' : token;
    config.headers['Userid'] = Userid === undefined ? '' : Userid;
    config.headers['roleCode'] = 2;
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    message.error(error);
    return Promise.reject(error);
  }
);

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    if (response.data.code === 300) {
      message.warning(response.data.msg);
    } else {
      return response.data;
    }
  },
  function (error) {
    // 对响应错误做点什么
    if (error && error.response) {
      switch (error.response.status) {
        case 408:
          message.error('登录过期,请重新登录');
          break;
        case 403:
          message.error('请求跨域');
          break;
        case 500:
          message.error('服务器内部错误');
          break;
        case 503:
          message.error("服务不可用");
          break;
        case 404:
          message.error('请求地址出错');
          break;
        case 400:
          message.error('请求错误');
          break;
        case 504:
          message.error('网关超时');
          break;


        default:
          message.error('其他错误');
          break;
      }
    } else {
      message.error('网络连接错误');
    }


  }
);

// get post请求封装  loading是否加载

export function get(url, options, loading) {
  const defaultOptions = {
    method: "get",
    url: baseUrl + url
  };
  options = options ? { ...defaultOptions, ...options } : defaultOptions;
  if (loading) {
    store.dispatch(setLoading({ loading: true }))
  }
  return new Promise((resolve, reject) => {
    axios(options).then(response => {
      if (response) {
        store.dispatch(setLoading({ loading: false }))
        resolve(response)
      } else {
        // reject(response);
      }
    })
  })
}

export function post(url, options, loading) {
  const defaultOptions = {
    method: "post",
    url: baseUrl + url
  };
  options = options ? { ...defaultOptions, ...options } : defaultOptions;
  if (loading) {
    store.dispatch(setLoading({ loading: true }))
  }
  return new Promise((resolve, reject) => {
    axios(options).then(response => {
      if (response) {
        store.dispatch(setLoading({ loading: false }))
        resolve(response)
      } else {
        store.dispatch(setLoading({ loading: false }))
        // reject(response);
      }

    })
  })
}

export function del(url, options, loading) {
  const defaultOptions = {
    method: "delete",
    url: baseUrl + url
  };
  options = options ? { ...defaultOptions, ...options } : defaultOptions;
  if (loading) {
    store.dispatch(setLoading({ loading: true }))
  }
  return new Promise((resolve, reject) => {
    axios(options).then(response => {
      if (response) {
        store.dispatch(setLoading({ loading: false }))
        resolve(response)
      } else {
        // reject(response);
      }
    })
  })
}
Component.prototype.get = get;
Component.prototype.post = post;
Component.prototype.delete = del;


/**
 * 新 axios 实例
 *
 * 请求参数参照原文档：https://github.com/axios/axios
 */
export function request(...args) {
  var instance = axios.create({
    baseURL: baseUrl,
    timeout: 10000,
  });

  instance.interceptors.request.use(
    function (config) {
      const token = localStorage.getItem('token');
      const Userid = localStorage.getItem('userId');
      config.headers['Token'] = token === undefined ? '' : token;
      config.headers['Userid'] = Userid === undefined ? '' : Userid;
      config.headers['roleCode'] = 2;
      return config;
    },
    function (error) {
      message.error(error);
      return Promise.reject(error);
    }
  );

  instance.interceptors.response.use(
    function (response) {
      if (response.data.code === 300) {
        message.warning(response.data.msg);
      } else {
        return response;
      }
    },
    function (error) {
      if (error && error.response) {
        switch (error.response.status) {
          case 400:
            message.error('请求错误');
            break;
          case 401:
            message.error('未检测到登录信息');
            break;
          case 403:
            message.error('请求被拒绝');
            break;
          case 404:
            message.error('请求地址出错');
            break;
          case 408:
            message.error('登录过期，请重新登录');
            break;
          case 500:
            message.error('服务器内部错误');
            break;
          case 503:
            message.error("服务不可用");
            break;
          case 504:
            message.error('网关超时');
            break;
          default:
            message.error('其他错误');
            break;
        }
      } else {
        message.error('网络连接错误');
      }
      return Promise.reject(error);
    }
  );

  return instance(...args);
};
